package com.centaur0.dwr;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import com.centaur0.io.PrintWriterWrapper;
import com.centaur0.io.ServletOutputStreamImpl;

public class DWRHttpServletResponse extends HttpServletResponseWrapper {

	private ServletOutputStream out;
	private PrintWriter printer;

	public DWRHttpServletResponse(HttpServletResponse response) {
		super(response);
		this.out = new ServletOutputStreamImpl();
		this.printer = new PrintWriterWrapper(out);
	}
	
	@Override
	public ServletOutputStream getOutputStream() throws IOException {
		return this.out;
	}
	
	@Override
	public PrintWriter getWriter() throws IOException {
		if(this.printer == null){
			return this.printer = new PrintWriter(this.out);
		} else{
			return this.printer;
		}
	}
	
	@Override
	public String toString() {
		return out.toString();
	}
}
